home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pcb / copydsc1.zip / COPYDESC.C next >
C/C++ Source or Header  |  1992-11-11  |  11KB  |  314 lines

  1. /*  COPYDESC version 1.01        Copyright (C) 1992 by Jim Robeson
  2.  *                                                     11/11/92
  3.  *  Function:
  4.  *    Read a PCBoard DIR LIST file (arg-1),
  5.  *      and copy it to a file (arg-2),
  6.  *      changing all multi-line descriptions to "long lines".
  7.  *      with the output position of the '|' set in arg-3,
  8.  *      and the starting position of the text set in arg-4.
  9.  *    note: requirment:   1 < arg-3 < arg-4 <= 34
  10.  *
  11.  *  Other uses:
  12.  *    None.
  13.  *
  14.  *  Execution:
  15.  *    Run from command line or .BAT:
  16.  *      COPYDESC drv:\path\in-file-name drv:\path\out-file-name |pos txtpos
  17.  *    If run without arguments, a bit of help appears.
  18.  *
  19.  *  Compiled with:
  20.  *    Borland's Turbo C 2.0.
  21.  *    tcc copydesc
  22.  *
  23.  *  Disclaimer:
  24.  *    This program is contributed to the Public Domain.  It can be
  25.  *    freely used, modified and/or distributed by anyone. The only
  26.  *    thing I ask is that you remember that I am not responsible
  27.  *    for anything that might go wrong through the use of this
  28.  *    program.  I have tested the program enough for my own use.
  29.  *    I also realize that bugs can appear in most every program.
  30.  *    Therefore, YOU USE THIS PROGRAM AT YOUR OWN RISK.
  31.  *
  32.  *  To-do:
  33.  *    > See if the blank in split-line hyphenated words can be eliminated.
  34.  *
  35.  *  Enjoy,
  36.  *  Jim Robeson
  37.  *  The Cricket BBS, Pacific Grove CA,  408-373-3773
  38.  */
  39.  
  40. #include "stdio.h"                        /* Standard I/O definitions */
  41. #include "string.h"
  42.  
  43. #define TRUE 1
  44. #define FALSE 0
  45.  
  46. /* test to see if 'ch' is a valid DOS character */
  47. #define vld_dos(ch)  ( (( (ch) >= 'A' && (ch) <= 'Z' ) || \
  48.                         ( (ch) >= '0' && (ch) <= '9' ) || \
  49.                         (ch) == '!' || \
  50.                         (ch) == '$' || \
  51.                         (ch) == '#' || \
  52.                         (ch) == '@')   \
  53.                        ? 1             \
  54.                        : 0 )
  55. /* test to see if 'ch' is numeric..... <compare   to isdigit(c)> */
  56. #define is_digit(ch)   ( ( (ch) < '0' || (ch) > '9' ) \
  57.                          ? 0                          \
  58.                          : ch )
  59.  
  60. void write_desc ( );             /* prototype */
  61. void save_desc ( );              /* prototype */
  62. void showhelp ( );               /* prototype */
  63.  
  64.   FILE *infile;                  /* the IN file */
  65.   FILE *outfile;                 /* the OUT file */
  66.   char inbuf[BUFSIZ];            /* line buffer for reading from file */
  67.                                  /* BUFSIZ = 512 in stdio.h */
  68.  
  69.   char outbuf[980];              /* Description Lines save area */
  70.         /* Lets start by assuming 12 max lines coming in. */
  71.         /* Therefore, we need on the output side:         */
  72.         /* a maximum of 80*12 = 960.                      */
  73.         /* Make it 980 for safety's sake!                 */
  74.         /* Watch this over run itself now!                */
  75.  
  76.   int in_a_desc;         /* working on a description indicator */
  77.   int ipt,opt,linept;    /* index pointers                     */
  78.   int pos_mark;          /* line position of the '|' character */
  79.   int pos_text;          /* starting position of output text   */
  80.  
  81. /* ========================================= */
  82. /* ===  MAIN                             === */
  83. /* ========================================= */
  84. main(int argc, char *argv[])                /* main reads command args  */
  85. {
  86.   char *progname;
  87.   char *filein;
  88.   char *fileout;
  89.   char *adr_mark;
  90.   char *adr_text;
  91.  
  92. /*  Display a little how-to "help" if arguments are null or improper  */
  93.  
  94.   if (argc != 5)           /*  should be 4 args + prognam */
  95.      { if (argc != 1)
  96.          printf("\nERROR: needs 4 arguments\n");
  97.        showhelp();
  98.        exit (1); }               /* exit with errorlevel=1 */
  99.  
  100.   progname = argv[0];
  101.   filein   = argv[1];
  102.   fileout  = argv[2];
  103.  
  104.   adr_mark = argv[3];   /* get arg-3: the position of the output '|' */
  105.   sscanf(&adr_mark[0],"%2d",&pos_mark);
  106.  
  107.   adr_text = argv[4];   /* get arg-4: the 1st position of output text */
  108.   sscanf(&adr_text[0],"%2d",&pos_text);
  109.  
  110.   if (pos_text >= 35)        /* 1st text position must be 34 or less */
  111.      { printf("\nERROR: text position must be less than 35. (arg-4)\n");
  112.        showhelp();
  113.        exit (1); }               /* exit with errorlevel=1 */
  114.  
  115.   if (pos_mark >= pos_text)  /* the '|' must be on the left of the text */
  116.      { printf("\nERROR: '|' position must be less than text position. (arg-3 or arg-4)\n");
  117.        showhelp();
  118.        exit (1); }               /* exit with errorlevel=1 */
  119.  
  120.   if (pos_mark <= 1)         /* the '|' must be 2 or higher */
  121.      { printf("\nERROR: '|' position is 1 or less. (arg-3)\n");
  122.        showhelp();
  123.        exit (1); }               /* exit with errorlevel=1 */
  124.  
  125.  
  126.   printf("\nCOPYDESC IS RUNNING.  Input = %s, Output = %s,\n",filein,fileout);
  127.   printf("                      mark = %2d, text = %2d.\n",pos_mark,pos_text);
  128.  
  129.   infile = fopen(filein,"r");          /* open the input file */
  130.   if (infile == NULL)
  131.     {
  132.     fprintf(stderr,"\n\007%s can't open the INPUT file: %s.\n",progname,filein);
  133.     exit (1);
  134.     }
  135.  
  136.   outfile = fopen(fileout,"w");          /* open the output file */
  137.   if (outfile == NULL)
  138.     {
  139.     fprintf(stderr,"\n\007%s can't open the OUTPUT file: %s.\n",progname,fileout);
  140.     exit (1);
  141.     }
  142.  
  143.   in_a_desc = FALSE;      /* say we're not inside a description yet */
  144.  
  145.  /* ========================  copy the file ========================= */
  146.   while ( fgets(inbuf,BUFSIZ,infile) != NULL )  /* TOP-OF-LOOP */
  147.     {
  148.     /* test for line 1 of a file description:     */
  149.     if (vld_dos(inbuf[0]) &&        /* pos 1 valid DOS chars & */
  150.         is_digit(inbuf[20]) &&      /* pos 21 is numeric       */
  151.         inbuf[25] == '-' &&         /* pos 26 = '-'            */
  152.         inbuf[28] == '-')           /* pos 29 = '-'            */
  153.  
  154.       { /* ==== FILE DESCRIPTION LINE # 1 FOUND ======== */
  155.  
  156.         /* if we're already in a description, write the old one */
  157.         if (in_a_desc) write_desc();
  158.         in_a_desc = TRUE;            /* say we are 'inside' */
  159.                                      /* save the 1st line   */
  160.         ipt = 0;
  161.         opt = 0;
  162.         while (inbuf[ipt] != '\0')
  163.           { outbuf[opt] = inbuf[ipt];
  164.             ipt++;
  165.             opt++;
  166.           }
  167.   /* ++++ add a blank to the end for wrap problem */
  168.         opt--;
  169.         while (outbuf[opt] == ' ' || outbuf[opt] == '\n')
  170.             opt--;
  171.         opt++;
  172.         outbuf[opt] = ' ';
  173.         opt++;
  174.         outbuf[opt] = '\n';
  175.         opt++;
  176.  /* ++++ */
  177.         outbuf[opt] = '\0';
  178.         linept = opt;       /* card pointer (1-80), pointing */
  179.         opt--;              /* at the '\n' right now.        */
  180.  
  181.       }   /* ====== end  FILE #1 LINE HERE =============== */
  182.  
  183.     else
  184.  
  185.       {   /* =========== OTHER LINES HERE ================ */
  186.  
  187.         /* if we're already in a description, continue saving data */
  188.         if (in_a_desc) save_desc();
  189.         else                       /* if not, write junk straight out */
  190.           fprintf(outfile,"%s",inbuf);
  191.  
  192.       }   /* ====== end  OTHER LINES HERE ================ */
  193.  
  194.  
  195.     } /* end of while at TOP-OF-LOOP */
  196.  
  197.   /* === Since we dropped through,  ============== */
  198.   /* === implies end of file found. ============== */
  199.  
  200.   if (in_a_desc) write_desc();  /* if we're in a description, write it */
  201.  
  202.   fclose(infile);
  203.   fclose(outfile);
  204.  
  205.   printf("COPYDESC IS FINISHED.\n");
  206.   exit (0);      /* job done, get out, errorvalue = 0 */
  207.  
  208. }  /* ------------- end of main ------------------------------- */
  209.  
  210. /* ========================================= */
  211. /* ===  SHOWHELP                         === */
  212. /* ========================================= */
  213. void showhelp ()
  214. {
  215.   printf("\nCOPYDESC:\n");
  216.   printf("    Copy PCBoard DIR files (descriptions) converting them to long-liners.\n");
  217.   printf("    by Jim Robeson, 11-05-92\n\n");
  218.   printf("USAGE:\n");
  219.   printf("    COPYDESC drv:\path\in-file-name drv:\path\out-file-name |pos txtpos \n");
  220.   printf("    where  1 < |pos < txtpos <= 34 \n\n");
  221.   return;
  222. }  /* ------------- end of showhelp --------------------------- */
  223.  
  224. /* ========================================= */
  225. /* ===  WRITE_DESC                       === */
  226. /* ========================================= */
  227. void write_desc ()
  228. {
  229.   fprintf(outfile,"%s",outbuf);
  230.   in_a_desc = FALSE;
  231.   return;
  232. }  /* ------------- end of write_desc ------------------------- */
  233.  
  234. /* ========================================= */
  235. /* ===  SAVE_DESC                        === */
  236. /* ========================================= */
  237. /* this is to save all but the first card into the output buffer */
  238. /* the first card is saved in MAIN */
  239. /*                 ipt goes 0-n thru input line, not flexible */
  240. /*                 opt goes 0-x thru output line, set/resets  */
  241. /*                     according to needs                     */
  242. /*                 linept is RELEATIVE to each OUTPUT LINE,   */
  243. /*                     ranging from 1-78 (PCBoard max);       */
  244. /*                     it gets reset as we fill lines.        */
  245. /*                     When coming from line 1, linept points */
  246. /*                     at the '\n' while opt = '\0'           */
  247. void save_desc ()
  248. {
  249.   int skipping;
  250.   int tmpipt;
  251.   int i;
  252.   skipping = TRUE;
  253.  
  254.   ipt = 0;
  255.   while (inbuf[ipt] != '\0')
  256.     {
  257.     if (inbuf[ipt] == '\n')
  258.        { ipt++;               /* skip over any '\n' */
  259.          continue; }
  260.     if (inbuf[ipt] == '|')
  261.        { ipt++;               /* skip over any '|' */
  262.          continue; }
  263.     if (inbuf[ipt] == ' ' && skipping)
  264.        { ipt++;               /* skip over leading blanks */
  265.          continue; }
  266.     skipping = FALSE;
  267.  
  268.     tmpipt = ipt;             /* see if there is room for a word */
  269.     for (i=0; !(inbuf[tmpipt]==' ' || inbuf[tmpipt]=='\n' || inbuf[tmpipt]=='\0' ); tmpipt++) i++;
  270.     if (i+linept > 79)
  271.        { /* do newline wrap first */
  272.        if (outbuf[opt] != '\n') outbuf[opt] = '\n';
  273.        opt++;
  274.        /* run out the '|' character & leading blanks */
  275.        for (linept=1; linept<pos_mark; linept++)
  276.          { outbuf[opt] = ' ';
  277.            opt++;
  278.          }
  279.        outbuf[opt] = '|';
  280.        opt++;
  281.        linept++;
  282.        for (; linept<pos_text; linept++)
  283.          { outbuf[opt] = ' ';
  284.            opt++;
  285.          }
  286.        }
  287.        /* now stuff the word */
  288.        for (; i >= 1; i--)               /* ++++ was >=0 */
  289.           { outbuf[opt] = inbuf[ipt];
  290.             ipt++;
  291.             opt++;
  292.             linept++;
  293.            }
  294.        /* now pop a blank IFF not '\n' nor '\0' */
  295.        if (inbuf[ipt]=='\n') continue;
  296.        if (inbuf[ipt]=='\0') continue;
  297.        for (; inbuf[ipt]==' '; ipt++);
  298.          outbuf[opt] = ' ';
  299.          opt++;
  300.          linept++;
  301.     }
  302.   outbuf[opt] = ' ';
  303.   opt++;
  304.   linept++;
  305.   outbuf[opt] = '\n';
  306.   opt++;
  307.   outbuf[opt] = '\0';
  308.   opt--;
  309.  
  310.   return;
  311. }  /* ------------- end of save_desc -------------------------- */
  312.  
  313. /*------------- End of COPYDESC.C ------------------------------------*/
  314.